/* Copyright Andy Mitchell 2007. No warranties */


function FilterEnvironment(gtdinboxPage){
try{
	this._gtdinboxPage = gtdinboxPage;
	this._doc = this._gtdinboxPage.doc;
	this._eventObservers = new EventObservers(this);
	
	this._eventObservers.observe( this._gtdinboxPage.gmailDOM.element("document").body, "contextmenu", this.contextMenu, true );
	this._eventObservers.observe( this._gtdinboxPage.mainDoc.body, "mouseup", this.mouseDown, true );
	this._eventObservers.observe( this._gtdinboxPage.mainDoc.body, "contextmenu", this.stopEvent, false );
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment constructor\n" + e.toString());
}
};


FilterEnvironment.prototype.createLabelPopup = function(labelName, el){
try{
	if( this._filterChild ){
		this._filterChild.destroy();
		this._filterChild = null;
	}
	
	var type = "Misc";
	var types = this._gtdinboxPage.settings.labels.types;
	for( t in types ){
		if( labelName.indexOf(types[t].prefix)==0 ){
			type = t;
			break;
		}
	}	
	
	var parentSearch = el.getAttribute("gtdSearch");
	if( !parentSearch ) parentSearch = GIPROTOTYPE.searchableLabel(labelName);
	this._filterChild = new FilterPopupUI( this._gtdinboxPage, labelName, type, el, [], [], parentSearch )
	this._filterChild.popup(el,{location:'right'});	
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment createLabelPopup\n" + e.toString());
}
};
FilterEnvironment.prototype.createContactPopup = function(name, email, el){
try{
	if( this._filterChild ){
		this._filterChild.destroy();
		this._filterChild = null;
	}
	//var f = this._gtdinboxPage.settings.filters["Contacts"];
	var search = "(";
	if( email ) search = "from:" + email + " OR to:" + email;
	if( name ) search += (search? " OR " : "") + "from:\"" + name + "\" OR to:\"" + name + "\"";
	search += ")";
	this._filterChild = new FilterPopupUI( this._gtdinboxPage, [name,email], "Contact", el, [], [], search )
	this._filterChild.popup(el,{location:'right'});
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment createContactPopup\n" + e.toString());
}
};

FilterEnvironment.prototype.viewChange = function(gmail){
try{
	if( this._elContactsBody ) return;
	
	var w = this._gtdinboxPage.window.frames[1];
	for( var i = 0; i < w.frames.length; i++ ){
		if( w.frames[i].location.href=="about:blank" ){
			var doc = w.frames[i].document;
			var onLoad = GIPROTOTYPE.bind(this, function(){
				if( this._elContactsBody ) return;
				this._elContactsBody = this._gtdinboxPage.gmailDOM.element("contacts-frame-body");
				if( this._elContactsBody ) this._eventObservers.observe( this._elContactsBody, "contextmenu", this.contextMenu, true );
				this._gtdinboxPage.addCSS(this._elContactsBody.ownerDocument);
			});
			this._eventObservers.observe( w.frames[i], "load", onLoad, false);
		}
	}	
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment viewChange\n" + e.toString());
}	
};

FilterEnvironment.prototype.contextMenu = function(event){	// called in regular
	this._processClick(event);
};
FilterEnvironment.prototype.mouseDown = function(event){	// called in im
	if( event.button!=2 ) return;
	this._processClick(event);
};
FilterEnvironment.prototype.stopEvent = function(event){	// called in im
try{
	if( this._extractNameEmail( event, GIPROTOTYPE.element(event) ) ) GIPROTOTYPE.stop(event);
}catch(e){
	if( GTDDEBUGMODE ) alert("contactfilter stopevent\n" + e.toString());
}
};
FilterEnvironment.prototype._processClick = function(event){
try{	
	var el = GIPROTOTYPE.element(event);
	var contact = this._extractNameEmail(event, el);
	if( contact ){
		GIPROTOTYPE.stop(event);
		this.createContactPopup(contact[0], contact[1], el);
	}else{
		var label = this._extractLabel(event, el);
		if( label ){
			GIPROTOTYPE.stop(event);
			this.createLabelPopup(label, el);
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("contactfilterpopuper mousedown\n" + e.toString());
}
};

FilterEnvironment.prototype._extractLabel = function(event, el){
try{
	var labelName = el.getAttribute("gtdLabelName");
	if( !labelName ){	// Try thread row item
		if( el.nodeName=="B" ){
			var i = 4;
			var elTree = el;
			while( i-->0 ){
				elTree = elTree.parentNode;
				if( elTree.className=="s75Nkf" && elTree.nodeName=="SPAN" ){
					labelName = GIPROTOTYPE.decodeEntities(el.innerHTML.replace(/<[^>]+>/g, ""));
					break;
				}
			}
		}
	}
	if( !labelName ){
		if( el.className=="MgLW8b" && el.nodeName=="SPAN" ){	// Conversation
			labelName = el.getAttribute("name");
		}
	}
	return labelName;
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment extractLabel\n" + e.toString());
}
};
FilterEnvironment.prototype._extractNameEmail = function(event, el){
try{
	var name = "";
	var email = "";
	if( email = el.getAttribute("email") ){
	}else if( el.className=="HHshnc" && el.nodeName=="SPAN" ){	// contacts box
		name = GIPROTOTYPE.decodeEntities(el.innerHTML.replace(/<[^>]+>/g, ""));
	}else if( el.className=="Gqb6V" && el.nodeName=="TD" ){	// contacts box
		var els = el.getElementsByTagName("SPAN");
		for( var i = 0; i < els.length; i++ ){
			if( els[i].className=="HHshnc" ){
				name = GIPROTOTYPE.decodeEntities(els[i].innerHTML.replace(/<[^>]+>/g, ""));
			}
		}
	}else if( el.className=="QO5lqe nRrh7" && el.nodeName=="DIV" && el.parentNode.nodeName=="TD" ){	// chat
		name = el.innerHTML;
	}else if( el.className=="text" && el.parentNode.className.indexOf("row")>-1 && el.previousSibling && el.previousSibling.className=="check" ){ // real contacts
		name = el.innerHTML;
		email = el.title;
	}
	if( name || email ){
		return [name, email];
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("FilterEnvironment extractName\n" + e.toString());
}
};

FilterEnvironment.prototype.destroy = function(){
	this._eventObservers.destroy();
	this._doc = null;
	this._gtdinboxPage = null;
		
	if( this._filterChild ){
		this._filterChild.destroy();
		this._filterChild = null;
	}
};

FilterEnvironment.prototype.createPopupArrow = function(el, labelName){
	// Create an arrow and return it
	// If el provided, 
	// If no labelname, try to extract it from el
};
